package com.cdu.service.impl;

import com.cdu.commons.R;
import com.cdu.mapper.AddressMapper;
import com.cdu.pojo.entity.Address;
import com.cdu.service.AddressService;
import com.cdu.commons.MallConstants;
import com.cdu.commons.ServiceCode;
import com.cdu.commons.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public R<Void> addAddress(Address address) {
        // 获取当前时间作为创建时间和修改时间
        Date currentDate = new Date();
        address.setCreatedTime(currentDate);
        address.setModifiedTime(currentDate);

        // 设置创建人、修改人，这里暂时写为固定值，实际可根据获取当前登录用户逻辑替换
        address.setCreatedUser("系统默认用户");
        address.setModifiedUser(address.getCreatedUser());

        // 调用Mapper层的方法插入地址数据
        int result = addressMapper.insertAddress(address);
        if (result!= 1) {
            throw new ServiceException("添加收货地址失败", ServiceCode.INSERT_ERROR);
        }
        return R.ok("收货地址添加成功");
    }
}